package cn.xerllent.common.service.impl;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;

import cn.xerllent.common.service.BaseService;

public abstract class BaseServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements BaseService<T> {

	/**
	 * pc端分页方法
	 * */
	protected void pageHelper(Pageable pageable) {
		String order = null;
		Sort sort = pageable.getSort();
        if (sort != null) {
            order = sort.toString();
            order = order.replace(":", "");
            if ("UNSORTED".equals(order)) {
                order = "";
            }
        }
        PageHelper.startPage(pageable.getPageNumber()>0?pageable.getPageNumber():1, pageable.getPageSize(), order);
    }
	
	/**
	 * 移动端偏移量分页,不可用，Pageable对象不能直接接收参数offset，他是从pageNo和pageSize计算而来
	 * */
	protected void pageHelper2(Pageable pageable) {
		String order = null;
		Sort sort = pageable.getSort();
        if (sort != null) {
            order = sort.toString();
            order = order.replace(":", "");
            if ("UNSORTED".equals(order)) {
                order = "";
            }
        }
        
        PageHelper.orderBy(order);
        PageHelper.offsetPage((int)pageable.getOffset(), pageable.getPageSize(), false);
    }
}
